package com.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

/**
 * @author：zhang guoxiang
 * @date：2022/4/15
 */
public abstract class JsonUtils {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     *
     * @param value 需要转换的数据
     */
    @SneakyThrows
    public static String toJsonStr(Object value) {
        return MAPPER.writeValueAsString(value);
    }

    /**
     *
     * @param value 需要转换的数据
     */
    @SneakyThrows
    public static byte[] toJsonBytes(Object value) {
        return MAPPER.writeValueAsBytes(value);
    }

    /**
     *
     * @param value 需要转换的json数据
     * @param tClass 转换class
     */
    @SneakyThrows
    public static <T> T readValue(String value, Class<T> tClass) {
        return MAPPER.readValue(value, tClass);
    }

    /**
     *
     * @param value 需要转换的json数据
     * @param reference 想要转换的数据类型
     */
    @SneakyThrows
    public static <T> T readValue(String value, TypeReference<T> reference) {
        return MAPPER.readValue(value, reference);
    }

}
